package com.ddd.demo.listviewdemo;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by doushuqi on 15/4/29.
 */
public class SimpleAdapterFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_main, container, false);
        final ListView lv = (ListView) view.findViewById(R.id.list_container);
        List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();

        for (int i = 0; i < 20; i++) {
            HashMap<String, Object> content = new HashMap<>();
            content.put("item title", "SimpleAdapter title");
            content.put("item no", i);
            content.put("item image", R.drawable.ic_launcher);
            list.add(content);
        }

        lv.setAdapter(new SimpleAdapter(getActivity(), list, R.layout.list_item,
                new String[]{"item title", "item no", "item image"},
                new int[]{R.id.title, R.id.number, R.id.iv
                }));
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), "" + position, Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }

}
